export default {
    getUrlParam(param) {
        /** 截取字符串 */
        var re = new RegExp('(\\?|&)' + param + '=([^&|^#]+)(&|#|$)', 'i');
        var m = window.location.href.match(re);
        if (m) {
            return decodeURIComponent(m[2]);
        } else {
            return '';
        }
    },
    /** 金额转换 */
    toMoney(num) {
        if (!num || num === 0) {
            return 0;
        }
        // var reg = /^\d*\.{0,1}\d{0,1}$/;
        num = parseFloat(num);
        num = num.toFixed(2);
        num = num.toLocaleString();
        // if (reg.test(num) && num > 0) {
        //   num = num + '0';
        // }
        if (num.indexOf('.') < 0) {
            num = num + '.00';
        }
        return num;
    },

    /** 金额转换 */
    getYuan(fen) {
        // var reg = /^\d*\.{0,1}\d{0,1}$/;
        if (!fen) {
            return 0.0;
        }
        fen = parseFloat(fen);
        var num = fen;
        num = fen * 0.01;
        num += '';
        var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g;
        num = num.replace(reg, '$1');
        var f = parseFloat(num);
        if (isNaN(f)) {
            return false;
        }
        var f1 = Math.round(num * 100) / 100;
        var s = f1.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2) {
            s += '0';
        }
        return s;
    },

    /** 表单验证月份 */
    validateMonth(rule, value, callback) {
        if (value === '' || !Number(value)) {
            callback(new Error('请输入正确月份'));
        } else if (
            Number.isInteger(Number(value)) &&
            Number(value) > 0 &&
            Number(value) < 13
        ) {
            callback();
        } else {
            callback(new Error('请输入正确月份'));
        }
    },

    /** 百分比验证 */
    validateTage(rule, value, callback) {
        const reg = /^(([0-9]{1}\d*)|(0{1}))(\.\d{0,2})?$/;
        if (value === '') {
            callback(new Error('请输入数字'));
        } else if (!reg.test(value)) {
            callback(new Error('请输入数字且保留两位小数'));
        } else if (value > 100) {
            callback(new Error('平台、商户应收之和不能超过100%'));
        } else if (value.split('.')[1] === '') {
            callback(new Error('请填写正确百分比'));
        }
        callback();
    },

    /** 表单验证日期 */
    validateDay(rule, value, callback) {
        if (value === '' || !Number(value)) {
            callback(new Error('请输入1至28之间的整数'));
        } else if (
            Number.isInteger(Number(value)) &&
            Number(value) > 0 &&
            Number(value) < 29
        ) {
            callback();
        } else {
            callback(new Error('请输入1至28之间的整数'));
        }
    },

    /** 表单 日期验证 **/
    validateNull(rule, value, callback) {
        if (!value || value === ' ') {
            callback(new Error('日期不能为空'));
        } else {
            callback();
        }
    },

    /** 表单验证 **/
    validateSequence(rule, value, callback) {
        if (value === '') {
            callback(new Error('请输入'));
        } else if (!Number.isInteger(+value)) {
            callback(new Error('输入数字'));
        } else {
            callback();
        }
    },

    /** 表单 日期验证 **/
    validateDaterange(rule, value, callback) {
        if (!value) {
            callback(new Error('日期不能为空'));
        } else {
            callback();
        }
    },

    /** 获取当前年月 */
    getYearMonth() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        month = month < 10 ? '0' + month : month;
        return `${year.toString()}-${month.toString()}`;
    },

    /** 导出excle */
    exportExcle(res, name, other) {
        // 创建Blob对象，设置文件类型
        let contentType;
        let suffix;
        switch (other) {
            case 'pdf':
                contentType = 'application/pdf;charset=UTF-8';
                suffix = 'pdf';
                break;
            case 'zip':
                contentType = 'application/octet-stream';
                suffix = 'zip';
                break;
            default:
                contentType = 'application/vnd.ms-excel';
                suffix = 'xlsx';
        }
        this.doExportExcle(res, contentType, res.headers['content-disposition'] ? this.excelNameHandler(decodeURI(res.headers['content-disposition'])) : name, suffix);
    },

    doExportExcle(res, contentType, fileName, suffix) {
        const link = document.createElement('a');
        const blob = new Blob([res.data], { type: contentType });
        const objectUrl = URL.createObjectURL(blob); // 创建URL
        link.href = objectUrl;
        link.download = `${fileName}.${suffix}`; // 自定义文件名
        link.click(); // 下载文件
        URL.revokeObjectURL(objectUrl); // 释放内存
    },

    excelNameHandler(name) {
        return name.replaceAll('attachment;fileName=', '').replaceAll('.xlsx', '').replaceAll('.xls', '')
            .replaceAll('.zip', '').replaceAll('.pdf', '');
    },

    /** 获取最近一周的日期 */
    fun_date(a) {
        // var time1;
        var time2;
        var date1 = new Date();
        // time1 = date1.getFullYear() + '-' + (date1.getMonth() + 1) + '-' + date1.getDate();
        var date2 = new Date(date1);
        date2.setDate(date1.getDate() + a);
        time2 =
            date2.getFullYear() +
            '-' +
            (date2.getMonth() + 1) +
            '-' +
            date2.getDate();
        return time2;
    },

    formatDate(date) {
        if (typeof date === 'string') {
            date = new Date(Date.parse(date.replace(/-/g, '/'))); //转换成Data();
        }
        var y = date.getFullYear();

        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? '0' + d : d;
        return y + '-' + m + '-' + d;
    },
    formatMonth(date) {
        if (typeof date === 'string') {
            date = new Date(Date.parse(date.replace(/-/g, '/'))); //转换成Data();
        }
        var y = date.getFullYear();

        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;

        return y + '-' + m;
    }

};